﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1.Actions
{
    public abstract class Action
    {
        // constants
        public const string PAGE_PARAM = "page";
        public const string RESULT_PER_PAGE_PARAM = "rp";
        public const string CURRENT_COUNT_ON_PAGE_PARAM = "currentCount";
        private const string DEFAULT_FALSE_PARAM = "false";
        public const int DEFAULT_RESULT_PER_PAGE = 15;
        public const string DEFAULT_SORT_COLUMN = "OID";
        public const string DEFAULT_SORT_ORDER = "desc";
        public const string ASCENDING_SORT_ORDER = "asc";
        public const string DESCENDING_SORT_ORDER = "desc";
        public const string ID = "id";
        public const string SORTNAME = "sortname";
        public const string SORTORDER = "sortorder";
        public const string QTYPE = "qtype ";
        public const string QUERY = "query ";

        public const string ENTITY_NOT_FOUND_ERROR_MESSAGE = "No entity found with this id or might have been deleted by someone!";
        public const string VERSION_CONFLICT_ERROR_MESSAGE = "The version you tried to edit has been updated by another user!";

        // for pagers
        public int PAGE_NUMBER = 1;
        public int CURRENT_COUNT = 0;
        public int RESULT_PER_PAGE = Action.DEFAULT_RESULT_PER_PAGE;
        public string SORT_COLUMN = Action.DEFAULT_SORT_COLUMN;
        public string SORT_ORDER = Action.DEFAULT_SORT_ORDER;
        public int START;

        // for search
        string queryType = string.Empty;
        string query = string.Empty;

        public void InitPager(FormCollection collection)
        {

            if (!string.IsNullOrEmpty(collection[PAGE_PARAM]))
            {
                PAGE_NUMBER = Convert.ToInt32(collection[PAGE_PARAM]);
            }

            if (!string.IsNullOrEmpty(collection[RESULT_PER_PAGE_PARAM]))
            {
                this.RESULT_PER_PAGE = Convert.ToInt32(collection[RESULT_PER_PAGE_PARAM]);
            }

            if (!string.IsNullOrEmpty(collection[SORTNAME]))
            {
                SORT_COLUMN = collection[SORTNAME];
            }

            if (!string.IsNullOrEmpty(collection[SORTORDER]))
            {
                SORT_COLUMN = collection[SORTORDER];
            }

            if (!string.IsNullOrEmpty(collection[CURRENT_COUNT_ON_PAGE_PARAM]))
            {
                CURRENT_COUNT = Convert.ToInt32(collection[CURRENT_COUNT_ON_PAGE_PARAM]);
            }


            // calculating the start offset
            START = ((PAGE_NUMBER - 1) * RESULT_PER_PAGE);
            if (START > 0)
            {
                START = START - (RESULT_PER_PAGE - CURRENT_COUNT);
            }
        }

        public void InitSearch(FormCollection collection)
        {
            InitPager(collection);
            queryType = collection[QTYPE];
            query = collection[QUERY];
        }
    }
}